// import 'dart:io';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
// import 'package:bitsdojo_window/bitsdojo_window.dart';

import 'package:ddlmanager/pages/ddl_list_page.dart';
import 'package:ddlmanager/pages/completed_ddl_list_page.dart';
import 'package:ddlmanager/pages/settings_page.dart';
import 'package:ddlmanager/backend/app_state.dart';
import 'package:ddlmanager/utils/theme_utils.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  var selectedIndex = 0;
  late dynamic appState;

  @override
  Widget build(BuildContext context) {
    appState = context.watch<AppState>();

    MaterialColor themeColor = getColorTheme();
    var lightTheme = ColorScheme.fromSeed(
      seedColor: themeColor,
      brightness: Brightness.light,
    );
    var darkTheme = ColorScheme.fromSeed(
      seedColor: themeColor,
      brightness: Brightness.dark,
    );
    Widget page;
    switch (selectedIndex) {
      case 0:
        page = DDLPage();
        break;
      case 1:
        page = CompletedDDLPage();
        break;
      case 2:
        page = SettingsPage();
        break;
      default:
        throw UnimplementedError('no widget for $selectedIndex');
    }

    return LayoutBuilder(
      builder: (context, constraints) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: getThemeData(AppState(), lightTheme),
          themeMode: AppState().settings.themeMode,
          darkTheme: getThemeData(AppState(), darkTheme),
          home: Scaffold(
            body: Row(
              children: [
                SafeArea(
                  child: NavigationRail(
                    extended: constraints.maxWidth >= 800,
                    destinations: [
                      NavigationRailDestination(
                        icon: Icon(Icons.home),
                        label: Text('DDL'),
                      ),
                      NavigationRailDestination(
                        icon: Icon(Icons.task),
                        label: Text('已完成'),
                      ),
                      NavigationRailDestination(
                        icon: Icon(Icons.settings),
                        label: Text('设置'),
                      ),
                    ],
                    selectedIndex: selectedIndex,
                    onDestinationSelected: (value) {
                      setState(() {
                        selectedIndex = value;
                      });
                    },
                  ),
                ),
                Expanded(
                  child: Container(
                    color: Theme.of(context).colorScheme.primaryContainer,
                    child: page,
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
